package com.huqingshan.LenMore.controller.api;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.huqingshan.LenMore.model.dto.ProductDTO;
import com.huqingshan.LenMore.model.entity.Product;
import com.huqingshan.LenMore.model.enums.SORTENUM;
import com.huqingshan.LenMore.service.ProductService;
import com.huqingshan.LenMore.utils.core.BeanUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @ApiOperation("获取所有产品")
    @SaCheckLogin()
    @GetMapping("")
    public List<ProductDTO> findAll(
            @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(name = "size", required = true, defaultValue = "4") Integer size,
            @RequestParam(value="sort",defaultValue = "DESC") SORTENUM sort
    ){

        return BeanUtils.mapAsList(productService.findAll(page, size), ProductDTO.class);
    }

    @ApiOperation("保存产品")
    @SaCheckLogin()
    @PostMapping("")
    public void Save(
            @RequestBody ProductDTO productDto)
    {
        productService.save(BeanUtils.map(productDto,Product.class));

    }

    @ApiOperation("根据产品名查询")
    @SaCheckLogin()
    @GetMapping(value = "",params = {"name"})
    public ProductDTO findByName(
            @RequestParam(value = "name") String name)
    {
        return BeanUtils.map(name, ProductDTO.class);
    }

}
